#!/bin/sh

die () { echo "$@" ; cleanup ; exit 1; }

WORK="$PWD/nvm_get_latest_failed_redirect-work.$$"
TEST_BIN="$WORK/bin"

cleanup() {
  rm -rf "$WORK"
  export PATH="$OLDPATH"
}

\. ../../../nvm.sh

\. ../../common.sh

OLDPATH="$PATH"

# fake curl/wget: `command curl` bypasses shell functions, so the mocks must be executables on PATH
make_fake_curl "$TEST_BIN" 'exit 1'
printf '#!/bin/sh\nexit 1\n' > "$TEST_BIN/wget"
chmod +x "$TEST_BIN/wget"

export PATH="$TEST_BIN:$OLDPATH"

try_err nvm_get_latest
[ "_$CAPTURED_STDERR" = "_https://latest.nvm.sh did not redirect to the latest release on GitHub" ] \
  || die "failed redirect did not report correct error message, got '$CAPTURED_STDERR'"
[ "_$CAPTURED_EXIT_CODE" = "_2" ] \
  || die "failed redirect did not exit with code 2, got $CAPTURED_EXIT_CODE"

cleanup
